<template>
  <div>
    <!-- 台桌表格 -->
    <div class="table">
      <el-table :data="lists" stripe style="width: 100%">
        <el-table-column prop="_id" label="ID" width="260" />
        <el-table-column prop="index" label="序号" width="250" sortable />
        <el-table-column prop="name" label="名称" width="250" />
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-link type="primary" @click="onRedact(scope.$index, scope.row)"
              >编辑</el-link
            >
            <span class="nbsb"></span>
            <el-link type="danger" @click="onDel(scope.$index, scope.row)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 编辑对话框 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form ref="table" :model="table" :rules="rules">
        <!--序号  -->
        <el-form-item label="序号" :label-width="formLabelWidth" prop="index">
          <el-input v-model="table.index" autocomplete="off"></el-input>
        </el-form-item>
        <!--名称  -->
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="table.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="ensure()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, deltable, updatedTable } from "@/api/table";
export default {
  data() {
    //   自定义验证
    let validateName = (rule, value, callback) => {
      if (value === "") {
        return callback("请输入台桌名称");
      }
      return callback();
    };
    let validateIndex = (rule, value, callback) => {
      if (value === "") {
        return callback("请输入台桌序号");
      }
      return callback();
    };
    return {
      lists: [],
      dialogFormVisible: false,
      formLabelWidth: "120px",
      name: "LLL",
      table: {
        name: "",
        index: 0,
        _listIndex: 0,
        id: "",
      },
      /* 添加校验规则 */
      rules: {
        name: [{ validator: validateName, trigger: "blur" }],
        index: [{ validator: validateIndex, trigger: "blur", type: "number" }],
      },
    };
  },
  mounted() {
    //   请求列表数据
    getList()
      .then((res) => {
        this.lists = res.data;
      })
      .catch((err) => {
        this.$message.error("请求失败");
      });
  },
  methods: {
    //   编辑
    onRedact(index, row) {
      // 显示退出编辑框
      this.dialogFormVisible = true;
      //   与弹出框数据绑定
      this.table.name = row.name;
      this.table.index = row.index;
      this.table._listIndex = index;
      this.table.id = row._id;
    },
    // 删除
    onDel(index, row) {
      this.$confirm("此操作将永久删除该台桌, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 发送删除请求
          deltable({ id: row._id })
            .then((res) => {
              // 删除列表中的数据
              this.lists.splice(index, 1);
              //   退出删除成功
              this.$message({
                type: "success",
                message: res.msg,
              });
            })
            .catch((err) => {
              this.$message.error("删除失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑-取消
    cancel() {
      // 关闭
      this.dialogFormVisible = false;
    },
    //编辑-确认
    ensure() {
      // 校验输入
      this.$refs["table"].validate((valid) => {
        //符合规则
        if (valid) {
          // 发起修改
          updatedTable(this.table)
            .then((res) => {
              //  弹出提示msg
              this.$message({
                message: res.msg,
                type: "success",
              });
              //   改变列表的值
              let index = this.table._listIndex;
              this.lists[index].index = res.data.index;
              this.lists[index].name = res.data.name;
              //   关闭编辑框
              this.dialogFormVisible = false;
            })
            .catch((err) => {
              this.$message.error("编辑错误！！");
            });
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.table {
  width: 100%;
  // 间隔
  .nbsb {
    display: inline-block;
    width: 10px;
  }
}
</style>